﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IBLL;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.APP.Classes;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class TR_ScholarshipFund : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        InitialData_BLL iniData = new InitialData_BLL();
        ITrainingScholarshipTypeBLL SchTypeBLL = new TrainingScholarshipTypeBLL();
        ITrainingScholarshipFundBLL SchFundBLL = new TrainingScholarshipFundBLL();
        private  Main main = new Main();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDataGrid();
                LoadScholaType();
            }
        }

        private void LoadDataGrid()
        {
            //DataSet ListSalaryCommand = iniData.ListScholarshipExpSupport("");
            RadGrid2.DataSource = SchFundBLL.GetFull();
            RadGrid2.DataBind();

        }

        protected void LoadScholaType()
        {
            cbScholaType.DataSource= SchTypeBLL.GetFull();
            cbScholaType.DataBind();
        }

        protected void RadGrid2_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "AddData")
            {
                ClearControl();
                UpdateUI("AddNew");
            }
            else if (e.CommandName == "Delete")
            {
                txtExpSeq.Text = RadGrid2.Items[e.Item.ItemIndex]["scholar_fund_seq"].Text;
                int UserID = ssMng.EmpSeqI;
                DateTime dtNow = DateTime.Now;
                UpdateUI("Save");
                TrainingScholarshipFundInfo info = new TrainingScholarshipFundInfo();
                info.scholar_fund_seq = Convert.ToInt32(RadGrid2.Items[e.Item.ItemIndex]["scholar_fund_seq"].Text);
                info.updated_by = UserID;
                info.updated_when = dtNow;
                SchFundBLL.Delete(info);
            }
            else if (e.CommandName == "DataEdit")
            {
                //GridDataItem item = (GridDataItem)e.Item;
                //txtExpSeq.Text = item["scholar_fund_seq"].Text;
                //txtExpDescr.Text = item["scholar_fund_descr"].Text;
                //cbScholaType.SelectedValue = item["scholar_type_seq"].Text;
                //string ScholaSeq = txtExpDescr.Text;

                txtExpSeq.Text = RadGrid2.Items[e.Item.ItemIndex]["scholar_fund_seq"].Text;

                //string strSchola = txtExpDescr.Text;
                //txtExpDescr.Text = RadGrid2.Items[e.Item.ItemIndex]["scholar_fund_descr"].Text;
                GridDataItem item = (GridDataItem)e.Item;
                txtExpDescr.Text = (item.FindControl("cmDetail") as LinkButton).Text;

                cbScholaType.SelectedValue = RadGrid2.Items[e.Item.ItemIndex]["scholar_type_seq"].Text;
                UpdateUI("Update");
            }
            
            LoadDataGrid();
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            int ScholaSeq = Convert.ToInt32(txtExpSeq.Text);
            string strSchola = txtExpDescr.Text;
            int? ScholaType = main.ConvertNullInt(cbScholaType.SelectedValue);
            int UserID = ssMng.EmpSeqI;
            DateTime dtNow = DateTime.Now;
            //UpdateUI("Update");
            TrainingScholarshipFundInfo info = new TrainingScholarshipFundInfo();
            info.scholar_fund_seq = ScholaSeq;
            info.scholar_fund_descr = strSchola;
            info.scholar_type_seq = ScholaType;
            info.created_by = UserID;
            info.created_when = dtNow;
            SchFundBLL.Update(info);
            UpdateUI("Cancel");
            LoadDataGrid();
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (checkValidate() == true)
            {
                string strSchola = txtExpDescr.Text;
                int ScholaType = Convert.ToInt32(cbScholaType.SelectedValue);
                int UserID = ssMng.EmpSeqI;
                DateTime dtNow = DateTime.Now;
                UpdateUI("Save");
                //ClearControl();
                TrainingScholarshipFundInfo info = new TrainingScholarshipFundInfo();
                info.scholar_fund_descr = strSchola;
                info.scholar_type_seq = ScholaType;
                info.created_by = UserID;
                info.created_when = dtNow;
                SchFundBLL.Insert(info);
                LoadDataGrid();
            }

        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            UpdateUI("Cancel");
            ClearControl();
        }
        protected void ClearControl()
        {
            txtExpDescr.Text = "";
            cbScholaType.SelectedIndex = 0;
        }
        protected bool checkValidate()
        {
            bool bOK = true;
            //string strScholaType = txtScholaType.Text;
            //if (strScholaType.Length == 0)
            //{
            //    bOK = false;
            //    lblError.Text = "* กรุณาใส่แหล่งทุน";
            //}
            return bOK;
        }
        protected void UpdateUI(string strCommand)
        {
            switch (strCommand)
            {
                case "Save":
                    PanelGrid.Visible = true;
                    PanelAddData.Visible = false;

                    break;

                case "AddNew":
                    PanelGrid.Visible = false;
                    PanelAddData.Visible = true;
                    btnUpdate.Visible = false;
                    btnSave.Visible = true;

                    break;

                case "Cancel":
                    PanelGrid.Visible = true;
                    PanelAddData.Visible = false;

                    break;

                case "Update":
                    PanelGrid.Visible = false;
                    PanelAddData.Visible = true;
                    btnSave.Visible = false;
                    btnUpdate.Visible = true;

                    break;
            }
        }
    }
}
